package com.czh.www.util;

import com.czh.www.domain.DataPacket;
import com.czh.www.domain.SocketManage;
import com.czh.www.domain.SystemVar;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NetUtils {

    public static boolean checkPort() {
        try {
            (new DatagramSocket(2425)).close();
            return true;
        } catch (SocketException var1) {
            return false;
        }
    }

    public static void sendUdpPacket(DataPacket dataPacket, String targetIp) {
        try {
            byte[] ex = dataPacket.toString().getBytes(SystemVar.DEFAULT_CHARACT);
            DatagramPacket sendPacket = new DatagramPacket(ex, ex.length, InetAddress.getByName(targetIp), 2425);
            SocketManage.getInstance().getUdpSocket().send(sendPacket);
        } catch (UnsupportedEncodingException var4) {
            Logger.getLogger(NetUtils.class.getName()).log(Level.SEVERE, (String) null, var4);
        } catch (IOException var5) {
            Logger.getLogger(NetUtils.class.getName()).log(Level.SEVERE, (String) null, var5);
        }

    }

    public static void broadcastUdpPacket(DataPacket dataPacket) {
        sendUdpPacket(dataPacket, "255.255.255.255");
    }

    public static String getLocalHostIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException var1) {
            Logger.getLogger(NetUtils.class.getName()).log(Level.SEVERE, (String) null, var1);
            return null;
        }
    }
}
